<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <el-form-item label="昵称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入昵称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入手机号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="认证状态" prop="authStatus">
        <el-select v-model="queryParams.authStatus" placeholder="请选择认证状态" clearable size="small">
          <el-option
            v-for="dict in authStatusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          :disabled="single"
          @click="handleAdd"
          v-hasPermi="['content:user:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['content:user:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['content:user:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['content:user:export']"
        >导出</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="userList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="认证状态" align="center" prop="id" />
      <el-table-column label="昵称" align="center" prop="name" />
      <!--  <el-table-column label="微信openid" align="center" prop="openId" /> -->
      <el-table-column label="手机号" align="center" prop="phone" width="120px;" />
      <el-table-column
        label="身份证前照"
        align="center"
        prop="idFront"
        header-align="center"
        width="120px;"
      >
        <template slot-scope="scope">
          <el-popover placement="right" title trigger="hover">
            <img :src="scope.row.idFront" style="max-height: 200px;max-width: 300px" />
            <img
              slot="reference"
              :src="scope.row.idFront"
              :alt="scope.row.idFront"
              style="max-height: 50px;max-width: 130px"
            />
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column label="身份证后照" align="center" prop="idBack" width="120px;">
        <template slot-scope="scope">
          <el-popover placement="right" title trigger="hover">
            <img :src="scope.row.idBack" style="max-height: 200px;max-width: 300px" />
            <img
              slot="reference"
              :src="scope.row.idBack"
              :alt="scope.row.idBack"
              style="max-height: 50px;max-width: 130px"
            />
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column
        label="企业执照"
        align="center"
        prop="businessLicense"
        header-align="center"
        width="120px;"
      >
        <template slot-scope="scope">
          <el-popover placement="right" title trigger="hover">
            <img :src="scope.row.businessLicense" style="max-height: 200px;max-width: 300px" />
            <img
              slot="reference"
              :src="scope.row.businessLicense"
              :alt="scope.row.businessLicense"
              style="max-height: 50px;max-width: 130px"
            />
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column label="认证状态" align="center" prop="authStatus" :formatter="authStatusFormat" />
      <el-table-column label="认证时间" align="center" prop="authTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.authTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="性别" align="center" prop="gender" :formatter="genderFormat" />
      <el-table-column label="认证失败原因" align="center" prop="authFail" width="120px;" />
      <el-table-column label="认证类型" align="center" prop="auth type" :formatter="authTypeFormat" />
      <el-table-column fixed="right" label="操作" width="200">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['content:user:edit']"
          >认证</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="auth(scope.row)"
            v-hasPermi="['content:user:edit']"
          >授权</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改用户认证对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <div style="margin: 25px;">
          <div style="display:inline-block;width: 100px;text-align:center;font-weight: bolder;">姓名</div>
          <div style="margin: 25px;display:inline-block;">{{form.name}}</div>
        </div>

        <div style="margin: 25px;">
          <div style="display:inline-block;width: 100px;font-weight: bolder;">身份证正面</div>

          <div class="demo-image__preview" style="display:inline-block">
            <el-image
              style="width: 320px; height: 200px;vertical-align:middle;"
              :src="idFrontUrl"
              :preview-src-list="idBackList"
            ></el-image>
          </div>
        </div>

        <div style="margin: 25px;">
          <div style="display:inline-block;width: 100px;font-weight: bolder;">身份证反面</div>
          <div class="demo-image__preview" style="display:inline-block">
            <el-image
              style="width: 320px; height: 200px;vertical-align:middle;"
              :src="idBackUrl"
              :preview-src-list="idBackList"
            ></el-image>
          </div>
        </div>
        <div style="margin: 25px;">
          <div style="display:inline-block;width: 100px;font-weight: bolder;">企业执照</div>
          <div class="demo-image__preview" style="display:inline-block">
            <el-image
              style="width: 320px; height: 200px;vertical-align:middle;"
              :src="businessLicensetUrl"
              :preview-src-list="businessLicenseList"
            ></el-image>
          </div>
        </div>
        <div style="margin: 25px;">
          <div style="display:inline-block;width: 100px;text-align:center;font-weight: bolder;">认证状态</div>
          <div style="margin: 25px;display:inline-block;">
            <el-select
              v-model="form.authtSatus"
              placeholder="请选择认证状态"
              @change="changeSatus(form.authtSatus)"
              clearable
              size="small"
            >
              <el-option
                v-for="dict in authStatusOptions"
                :key="dict.dictValue"
                :label="dict.dictLabel"
                :value="dict.dictValue"
              />
            </el-select>
          </div>
        </div>
        <div style="margin: 25px;" v-if="form.authtSatus == 2 ">
          <div style="width: 100px;text-align:center;font-weight: bolder;">审核失败原因</div>
          <div style="margin: 15px;margin-left: 100px;">
            <el-input :rows="4" type="textarea" style="width: 301px;" v-model="form.authFail"></el-input>
          </div>
        </div>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog :title="title" :visible.sync="dialogFlag" width="600px" append-to-body>
      <div class="el-switch-user">
        <el-switch v-model="value1" @change="change1" active-text="开启发圈" inactive-text="开启发圈"></el-switch>
        <p style="color:red">开启之后重启小程序即可使用发圈功能</p>
      </div>
       
   <div class="el-switch-user">
  <el-switch
        style="display: block"
        v-model="value2"
        @change="change2"
        active-text="开启发布直播"
        inactive-text="开启发布直播"
      ></el-switch>
      <p style="color:red">开启之后重启小程序即可使用发布直播功能</p>
   </div>
      <div class="el-switch-user">
 <el-switch
        style="display: block"
        v-model="value3"
        @change="change3"
        active-text="开启发布商品"
        inactive-text="开启发布商品"
      ></el-switch>
       <p style="color:red">开启之后重启小程序即可使用发布商品功能</p>
      </div>
    

     

    </el-dialog>
  </div>
</template>
<script>
import {
  listUser,
  getUser,
  delUser,
  addUser,
  updateUser,
  exportUser,
} from "@/api/content/user";

export default {
  name: "User",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 用户认证表格数据
      userList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 认证状态1是认证2是拒绝0是未认证3是等待认证字典
      authStatusOptions: [],
      // 1是男 2是女 0是未知字典
      genderOptions: [],
      // 认证类型: 1 个人 2 企业 3 事业单位字典
      authTypeOptions: [],
      flag: "",
      disabledOption: true,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: "",
        phone: "",
        authStatus: "",
      },
      // 表单参数
      form: {},
      //身份证背面
      idBackUrl: "",
      idBackList: [],
      //身份证正面
      idFrontUrl: "",
      idFrontList: [],
      //企业执照
      businessLicensetUrl: "",
      businessLicenseList: [],
      // 表单校验
      rules: {
        hasEdit: [
          { required: true, message: "信息已编辑不能为空", trigger: "blur" },
        ],
      },
      dialogFlag: false,
      value1: false,
      value2: false,
      value3: false,
    };
  },
  created() {
    this.getList();
    this.getDicts("sys_auth_status").then((response) => {
      this.authStatusOptions = response.data;
    });
    this.getDicts("sys_user_sex").then((response) => {
      this.genderOptions = response.data;
    });
    this.getDicts("sys_auth_type").then((response) => {
      this.authTypeOptions = response.data;
    });
  },
  methods: {
    /** 查询用户认证列表 */
    getList() {
      this.loading = true;
      listUser(this.queryParams).then((response) => {
        this.userList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 认证状态1是认证2是拒绝0是未认证3是等待认证字典翻译
    authStatusFormat(row, column) {
      return this.selectDictLabel(this.authStatusOptions, row.authStatus);
    },
    // 1是男 2是女 0是未知字典翻译
    genderFormat(row, column) {
      return this.selectDictLabel(this.genderOptions, row.gender);
    },
    // 认证类型: 1 个人 2 企业 3 事业单位字典翻译
    authTypeFormat(row, column) {
      return this.selectDictLabel(this.authTypeOptions, row.auth_type);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.flag = 0;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: "",
        name: "",
        openId: "",
        email: "",
        phone: "",
        idNo: "",
        idFront: "",
        idBack: "",
        businessLicense: "",
        authStatus: "",
        createTime: "",
        authTime: "",
        wxHeadPhoto: "",
        nickname: "",
        password: "",
        type: "",
        hasAuth: "",
        isImport: "",
        address: "",
        nowAddress: "",
        isVip: "",
        vipLevel: "",
        medal: "",
        account: "",
        accountIndex: "",
        userinfo: "",
        gender: "",
        isLock: "",
        field: "",
        fieldId: "",
        hasEdit: "",
        isAllowLive: "",
        isAllowPublishLive: "",
        authFail: "",
        auth_type: "",
      };
      this.resetForm("form");
    },
    changeSatus(va) {
      this.form.authStatus = va;
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加用户认证";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id;
      getUser(id).then((response) => {
        this.form = response.data;
        this.setUrl();
        this.open = true;
        this.title = "用户认证";
      });
    },
    auth(row) {
      this.reset();
      const id = row.id;
      getUser(id).then((response) => {
        this.form = response.data;
        console.log(response.data);
        this.setUrl();
        this.dialogFlag = true;
        if (this.form.hasMyWorld) {
          this.value1 = true;
        }
        if (this.form.hasLive) {
          this.value2 = true;
        }
        if (this.form.hasYsp) {
          this.value3 = true;
        }
        this.title = "用户授权";
      });
    },
    //开启发圈  开启发布直播  开启发布商品
    change1(e) {
      console.log(e);
      if (e) {
        this.form.hasMyWorld = 1;
      } else {
        this.form.hasMyWorld = 0;
      }

      updateUser(this.form).then((response) => {
        if (response.code === 200) {
          this.msgSuccess("修改成功");
          this.open = false;
          this.getList();
          this.flag = 0;
        } else {
          this.msgError(response.msg);
        }
      });
    },
    change2(e) {
      console.log(e);
      if (e) {
        this.form.hasLive = 1;
      } else {
        this.form.hasLive = 0;
      }
      updateUser(this.form).then((response) => {
        if (response.code === 200) {
          this.msgSuccess("修改成功");
          this.open = false;
          this.getList();
          this.flag = 0;
        } else {
          this.msgError(response.msg);
        }
      });
    },
    change3(e) {
      console.log(e);
      if (e) {
        this.form.hasYsp = 1;
      } else {
        this.form.hasYsp = 0;
      }
      updateUser(this.form).then((response) => {
        if (response.code === 200) {
          this.msgSuccess("修改成功");
          this.open = false;
          this.getList();
          this.flag = 0;
        } else {
          this.msgError(response.msg);
        }
      });
    },
    setUrl() {
      //身份证背面
      (this.idBackUrl = this.form.idBack),
        (this.idBackList = [this.form.idBack, this.form.idBack]),
        //身份证正面
        (this.idFrontUrl = this.form.idFront),
        (this.idFrontList = [this.form.idFront, this.form.idFront]),
        //企业执照
        (this.businessLicensetUrl = this.form.businessLicense),
        (this.businessLicenseList = [
          this.form.businessLicense,
          this.form.businessLicense,
        ]);
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != "") {
            updateUser(this.form).then((response) => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
                this.flag = 0;
              } else {
                this.msgError(response.msg);
              }
            });
          } else {
            addUser(this.form).then((response) => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              } else {
                this.msgError(response.msg);
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm(
        '是否确认删除用户认证编号为"' + ids + '"的数据项?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(function () {
          return delUser(ids);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(function () {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm("是否确认导出所有用户认证数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return exportUser(queryParams);
        })
        .then((response) => {
          this.download(response.msg);
        })
        .catch(function () {});
    },
  },
};
</script>

<style rel="stylesheet/scss" lang="scss">
.el-switch-user{
   margin: 25px;
}
</style>